/**
 *
 */
package com.jk.vehicle.core.context;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 应用上下文
 *
 * @author KangJian
 * @date 2018/7/19
 */
@Component
public class ApplicationContextHolder implements ApplicationContextAware {

	/**
	 * Spring上下文
	 */
	static ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		ApplicationContextHolder.applicationContext = applicationContext;
	}

	/**
	 * 获取Bean
	 * @param t
	 * @param <T>
	 * @return
	 */
	public static <T> T getBean(Class<T> t) {
		return applicationContext.getBean(t);
	}

	/**
	 * 获取Bean类型
	 * @param t
	 * @param <T>
	 * @return
	 */
	public static <T> Map<String, T> getBeansOfType(Class<T> t) {
		return applicationContext.getBeansOfType(t);
	}

	/**
	 * 根据名称获取Bean
	 * @param name
	 * @return
	 */
	public static Object getBean(String name) {
		return applicationContext.getBean(name);
	}
}
